package com.ailiwu.www.ailiwu.adapter;

import android.content.Context;
import android.graphics.Bitmap;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.ailiwu.www.ailiwu.R;
import com.ailiwu.www.ailiwu.base.MyApplication;
import com.ailiwu.www.ailiwu.bean.Comment;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.ImageLoaderConfiguration;
import com.nostra13.universalimageloader.core.assist.ImageScaleType;
import com.nostra13.universalimageloader.core.display.RoundedBitmapDisplayer;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;

/**
 * Author:    Yyang
 * Version    V1.0
 * Date:      ${date}  ${time}.
 * Description:
 * Modification  History:
 * Date         	Author        		Version        	Description
 * -----------------------------------------------------------------------------------
 * ${date}          Yyang                1.0                1.0
 * Why & What is modified:
 */
public class CommentRecyclerViewAdapter extends RecyclerView.Adapter<CommentRecyclerViewAdapter.MyViewHolder> {
    private final DisplayImageOptions options;
    private Context context;
    private LayoutInflater layoutInflater;
    private ArrayList<Comment.DataEntity.CommentsEntity> commentsList;
    private ItemClickListener itemClickListener;
    private final int ONEDAY = 86400000;
    private final int SEVENDAY = 604800000;

    public CommentRecyclerViewAdapter(Context context, ArrayList<Comment.DataEntity.CommentsEntity> commentsList) {
        this.context = context;
        this.commentsList = commentsList;
        layoutInflater = LayoutInflater.from(context);
        // 使用DisplayImageOptions.Builder()创建DisplayImageOptions
        options = new DisplayImageOptions.Builder()
                .showImageOnLoading(R.mipmap.me_avatar_boy)         // 设置图片在下载期间显示的图片
                .showImageForEmptyUri(R.mipmap.me_avatar_boy)       // 设置图片Uri为空或是错误的时候显示的图片
                .showImageOnFail(R.mipmap.me_avatar_boy)            // 设置图片加载/解码过程中错误时候显示的图片
                .bitmapConfig(Bitmap.Config.RGB_565)                // 设置图片的解码类型
                .imageScaleType(ImageScaleType.EXACTLY_STRETCHED)   // 设置图片以如何的编码方式显示
                .cacheInMemory(true)                                // 设置下载的图片是否缓存在内存中
                .cacheOnDisk(true)                                  // 设置下载的图片是否缓存在SD卡中
                .displayer(new RoundedBitmapDisplayer(90))          // 设置成圆角图片
                .build();                                           // 创建配置过得DisplayImageOption对象

    }

    @Override
    public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = layoutInflater.inflate(R.layout.item_recycler_instroduce, parent, false);
        return new MyViewHolder(view, itemClickListener);
    }

    @Override
    public void onBindViewHolder(MyViewHolder holder, int position) {
        Comment.DataEntity.CommentsEntity data = commentsList.get(position);
        holder.tv_nickname.setText(data.user.nickname);//设置姓名

        //设置评论内容:判断是否是回复他人的评论
        if (data.reply_to_id == 0) {
            holder.tv_content.setText(data.content);
        } else {
            holder.tv_content.setText("回复" + data.replied_user.nickname + ":" + data.content);
        }

        ImageLoader imageLoader = ImageLoader.getInstance();

        Log.i("oye", "" + data.user.avatar_url.indexOf("liwushuo"));
        if (data.user.avatar_url.indexOf("liwushuo") >= 0) {
            imageLoader.init(ImageLoaderConfiguration.createDefault(MyApplication.mContext));
            imageLoader.displayImage(data.user.avatar_url, holder.iv_avatar, options);
        } else {
            holder.iv_avatar.setImageResource(R.mipmap.me_avatar_boy);
        }
        holder.tv_created_at.setText(getCreatTime((long) data.created_at));
    }

    @Override
    public int getItemCount() {
        return commentsList == null ? 0 : commentsList.size();
    }

    /**
     * 传入评论时间格式化成字符串返回
     * @param createdTime
     * @return
     */
    public String getCreatTime(long createdTime) {
        SimpleDateFormat formatter1 = new SimpleDateFormat("MM月dd日 , HH:mm");
        Calendar calendar = Calendar.getInstance();
        calendar.setTimeInMillis(createdTime * 1000);//用给定的 long 值设置此 Calendar 的当前时间值。
        Date date = calendar.getTime();// 返回Data对象,可用于格式化
        return formatter1.format(date);//格式化成:"MM月dd日 , HH:mm"

    }


    public class MyViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
        private ImageView iv_avatar;
        private TextView tv_nickname, tv_content, tv_created_at;
        private ItemClickListener itemClickListener = null;

        public MyViewHolder(View itemView, ItemClickListener ItemClickListener) {
            super(itemView);
            this.itemClickListener = ItemClickListener;  //监听的具体事件通过Adapter创建ViewHolder时传递进来

            itemView.setOnClickListener(this);           // 对每个itemView设置点击监听

            iv_avatar = (ImageView) itemView.findViewById(R.id.iv_avatar);
            tv_nickname = (TextView) itemView.findViewById(R.id.tv_nickname);
            tv_content = (TextView) itemView.findViewById(R.id.tv_content);
            tv_created_at = (TextView) itemView.findViewById(R.id.tv_created_at);
        }

        @Override
        public void onClick(View v) {
            if (itemClickListener != null) {
                itemClickListener.onItemClick(v, getPosition());
            }
        }
    }

    // 点击监听接口
    public interface ItemClickListener {
        void onItemClick(View view, int postion);
    }

    /**
     * 设置监听事件具体内容的方法
     *
     * @param listener 监听事件具体内容
     */
    public void setItemClickListener(ItemClickListener listener) {
        this.itemClickListener = listener;
    }

}
